/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.webapp.controller;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jrose.entity.data.User;
import org.jrose.webapp.security.SecurityContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Abstract controller implementation.
 *
 * @author Xiaohai Zhang
 * @since 2013-09-05 21:08
 */
@NoArgsConstructor(access = AccessLevel.PROTECTED)
abstract public class AbstractController extends ExposedServiceProvider {
    protected HttpServletRequest currentRequest() {
        SecurityContext context = SecurityContext.getCurrentSecurityContext().get();
        return context == null ? null : context.getRequest();
    }

    protected String requestParameter(String name) {
        HttpServletRequest request = currentRequest();
        if (request == null) {
            return "";
        } else {
            return StringUtils.defaultString(request.getParameter(name));
        }
    }

    protected User currentUser() {
        SecurityContext context = SecurityContext.getCurrentSecurityContext().get();
        return context == null ? null : context.getUser();
    }
}
